package com.sumer.psychology.comment.domain.model;

import com.sumer.psychology.common.domain.model.Author;

import java.util.Date;

/**
 * 有评分的评论
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/25
 * Time: 10:53
 */
public class ScoredComment extends Comment {
    private Integer score;

    /**
     * 创建带评分的评论
     *
     * @param identity id
     * @param host     host
     * @param content  content
     * @param author   author
     * @param type     type
     * @param score    score
     */
    public ScoredComment(CommentId identity, CommentHost host, String content, Author author, CommentType type, Integer score) {
        super();
        initialize(identity, host, content, author, type);
        setScore(score);

        publishCreateEvent();
    }

    /**
     * 创建带评论的评分
     *
     * @param aComment comment
     * @param score    score
     */
    public ScoredComment(Comment aComment, Integer score) {
        super();
        initialize(aComment.commentId(),
                aComment.host(),
                aComment.content(),
                aComment.author(),
                aComment.commentType());
        setScore(score);

        publishCreateEvent();
    }

    public ScoredComment() {
    }

    /*
     * 创建事件
     */
    private void publishCreateEvent() {
        apply(new ScoredCommentCreatedEvent(
                commentId(),
                host(),
                content(),
                author(),
                commentType(),
                score()));
    }

    /**
     * 修改评分和评论内容
     *
     * @param content content
     * @param score   score
     */
    public void change(String content, int score) {
        setContent(content);
        setScore(score);
        setLastModifyTime(new Date());

        apply(new ScoredCommentChangedEvent(
                commentId(),
                content(),
                score()));
    }

    public Integer score() {
        return score;
    }

    private void setScore(Integer score) {
        this.score = score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ScoredComment comment = (ScoredComment) o;

        if (author() != null ? !author().equals(comment.author()) : comment.author() != null) return false;
        if (availability() != comment.availability()) return false;
        if (commentId() != null ? !commentId().equals(comment.commentId()) : comment.commentId() != null) return false;
        if (commentType() != comment.commentType()) return false;
        if (content() != null ? !content().equals(comment.content()) : comment.content() != null) return false;
        if (host() != null ? !host().equals(comment.host()) : comment.host() != null) return false;
        if (score != null ? !score.equals(comment.score()) : comment.score() != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = commentId() != null ? commentId().hashCode() : 0;
        result = 31 * result + (host() != null ? host().hashCode() : 0);
        result = 31 * result + (content() != null ? content().hashCode() : 0);
        result = 31 * result + (author() != null ? author().hashCode() : 0);
        result = 31 * result + (commentType() != null ? commentType().hashCode() : 0);
        result = 31 * result + (availability() != null ? availability().hashCode() : 0);
        result = 31 * result + (score() != null ? score().hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "commentId=" + commentId() +
                ", host=" + host() +
                ", content='" + content() + '\'' +
                ", author=" + author() +
                ", commentType=" + commentType() +
                ", createTime=" + createTime() +
                ", lastModifyTime=" + lastModifyTime() +
                ", availability=" + availability() +
                ", score=" + score() +
                '}';
    }
}
